package com.jy.adscm.operation.yx.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.yx.bean.Agents;
import com.jy.adscm.operation.yx.bean.QueryAgents;
import com.jy.adscm.operation.yx.service.AgentsService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-24
 */
@RestController
@RequestMapping("adscm/operation/yx/agents")
public class AgentsController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    AgentsService agentsService;

    /**
     * 增加bom信息
     * @param agents
     * @param request
     * @return
     */
    @AdscmLogger("增加三类代理信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody Agents agents, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        boolean insert = agentsService.insert(agents);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改bom信息
     * @param agents
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改三类代理信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody Agents agents, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean result = agentsService.updateById(agents);
        return  new SimpleCoreResponse("200","修改成功",result);
    }


    @RequestMapping("select")
    public  SimpleCoreResponse  select(@RequestBody QueryAgents queryAgents, HttpServletRequest request) throws  AdscmSystemException{
        Page<Agents>  page =new Page<>(queryAgents.getCurrentPage(),queryAgents.getPageSize());
        page = agentsService.selectAgents(page,queryAgents);
        return  new SimpleCoreResponse("200","查询成功",page);
    }




}

